/**
 * 文件名称:  DemoServiceImpl.java
 * 文件序号:  序号
 * 内容描述:  文件简单描述，多行换行书写
 * 版权声明:  山东鸿蝶信息技术有限公司
 * 创建用户:  zhangkai
 * 创建日期:  2016/12/21 10:02
 * 修改用户:  无
 * 修改日期:  无
 * 修改说明:  无
 * 文件版本:  V1.0
 * 文件路径:
 */

package com.hongdee.edm.demo.service.impl;


import com.hongdee.common.dao.IHibernateDao;
import com.hongdee.common.dao.IHibernateTemplateDao;
import com.hongdee.common.dao.IJdbcDao;
import com.hongdee.edm.demo.dao.IDemoEntityJpa;
import com.hongdee.edm.demo.entity.DemoEntity;
import com.hongdee.edm.demo.service.IDemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * 名  称:DemoServiceImpl
 * 类  型: class
 * 描  述: 用一句话描述类的作用
 * 路  径:com.hongdee.demo.service.impl
 * 作  者:zhangkai
 * 日  期:2016/12/21 10:02
 */
@Service
public class DemoServiceImpl<T> implements IDemoService<T> {

    @Autowired
    private IJdbcDao jdbcDao;

    @Autowired
    private IHibernateDao hibernateDao;


    @Autowired
    private IDemoEntityJpa iDemoEntityJpa;

    @Autowired
    private IHibernateTemplateDao hibernateTemplateDao;

    @Override
    public List query() throws Exception {
        throw new Exception("查询数据库出错");
        //return this.jdbcDao.queryForMapListBySql("select * from T_DEMO_ENTITY");
    }

    @Override
    public List queryHql() throws Exception {
        String hql = "from DemoEntity";

        return this.hibernateDao.findEntityListByHql(hql);
    }

    @Override
    public String add() throws Exception {
        DemoEntity dm = new DemoEntity();
        dm.setName("Demo测试");
        this.hibernateDao.addEntity(dm);
        return dm.getID();
    }

    @Override
    @Transactional
    public String addJpa() {
        DemoEntity dm = new DemoEntity();
        dm.setName("JpaDemo测试");
        this.iDemoEntityJpa.save(dm);
        DemoEntity dm2 = new DemoEntity();
        dm2.setName("JPADemo@");
        this.iDemoEntityJpa.save(dm2);
        DemoEntity dm3 = new DemoEntity();
        dm3.setName("JPA");
        this.iDemoEntityJpa.save(dm3);
        return dm.getID();

    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
    public Serializable addTemplate() throws Exception {
        DemoEntity dm = new DemoEntity();
        dm.setName("imya HibernateTemplate");
        this.hibernateDao.addEntity(dm);
        return dm.getID();
    }

    @Override
    public List queryJpa() {
        return this.iDemoEntityJpa.findAll();
    }

    @Override
    public String saveTemplate(T entity) throws Exception {
        DemoEntity dm = new DemoEntity();
        dm.setName("TempalteDao");
        return (String) this.hibernateTemplateDao.addEntity(dm);
    }


}
